﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ScheduleSystem_Tasks_Taskslist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        Task MyTask = new Task();
        String mySqlStatment="";
        var role = System.Web.Security.Roles.GetRolesForUser().Single();
        if (User.IsInRole("Administrator"))
         mySqlStatment = "select Name,StartDate as 'Start Date' ,DueDate as 'Due Date', Complete, Description, Priority, Status," +
            "AssignementDate as 'Assignment date' , Users.UserName as 'Employee', Costumer.UserName as 'Customer'" +
        "from Task,Users, Users Costumer " +
        " Where Users.UserId = Task.UserId and Costumer.UserId = Task.CostumerId";
        else if (User.IsInRole("Customer"))
            mySqlStatment = "select Name,StartDate as 'Start Date' ,DueDate as 'Due Date', Complete, Description, Priority, Status," +
           "AssignementDate as 'Assignment date' , Users.UserName as 'Employee'" +
        "from Task,Users" +
        " Where Task.UserId = Users.UserId   and Task.CostumerId = '" + Membership.GetUser().ProviderUserKey + "'";
        else
            mySqlStatment = "select Name,StartDate as 'Start Date' ,DueDate as 'Due Date', Complete, Description, Priority, Status," +
          "AssignementDate as 'Assignment date' , Users.UserName as 'Employee', us.UserName as 'Customer'" +
       "from Task,Users, Users us " +
       " Where Users.UserId = Task.UserId and us.UserId = Task.CostumerId";

        TasksList.DataSource = MyTask.GetTasks(mySqlStatment);
        TasksList.DataBind();

    }
    protected void Taskslist_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void SortData(object sender, GridViewSortEventArgs e)
    {
 

    //Retrieve the table from the session object.
        DataTable dt = Session["MyTask"] as DataTable;

    if (dt != null)
    {

      //Sort the data.
      dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
      TasksList.DataSource = Session["TaskTable"];
      TasksList.DataBind();
    }



    }
    private string GetSortDirection(string column)
    {

        // By default, set the sort direction to ascending.
        string sortDirection = "ASC";

        // Retrieve the last column that was sorted.
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {
            // Check if the same column is being sorted.
            // Otherwise, the default value can be returned.
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) && (lastDirection == "ASC"))
                {
                    sortDirection = "DESC";
                }
            }
        }

        // Save new values in ViewState.
        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }


    protected void FormatCells(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // assuming the first cell hols a date string
            e.Row.Cells[1].Text = DateTime.Parse(e.Row.Cells[1].Text).ToString("MM/dd/yyyy");
            e.Row.Cells[2].Text = DateTime.Parse(e.Row.Cells[2].Text).ToString("MM/dd/yyyy");
            e.Row.Cells[7].Text = DateTime.Parse(e.Row.Cells[1].Text).ToString("MM/dd/yyyy");
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Calendar.aspx");
    }
}